<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Modcontact extends MX_Controller{
    public function __construct(){
        parent::__construct();
        $this->load->helper("form");
        $this->load->library(array("form_validation","email"));
    }
    public function index(){
        echo "Hello world!";
    }
    public function sendMail(){
        $this->load->model(array("modcontact/mmodcontact_site_contact_email","modcontact/mmodcontact_site_text"));
        $data['static_page'] = $this->mmodcontact_site_text->getDataByID(6);
        
        $this->form_validation->set_rules("name","Họ tên","required");
        $this->form_validation->set_rules("email","Email","required");
        $this->form_validation->set_rules("content","Nội dung","required|min_length[20]");
        
        if($this->form_validation->run() == FALSE){
            $data['successfully'] = "";
            $data['error'] = ""; 
            $this->load->view("modcontact/modcontact-sendMail",$data);
        }else{
            $data['email'] = $this->mmodcontact_site_contact_email->getStatusData(1,"ASC",1,0);
            
            $email_content = $data['email']['Content'];
    		$marText1=array("{contact_name}","{contact_email}","{contact_phone}","{contact_address}","{contact_content}");
    		$marText2=array($this->input->post("name"),$this->input->post("email"),$this->input->post("phone"),$this->input->post("address"),str_replace("\r\n","<br/>",$this->input->post("content",TRUE)));
            $email_content = strtolower(str_replace($marText1, $marText2, $email_content));
            
            $this->email->clear();
            $this->email->from($this->input->post("email"), $this->input->post("name"));
            $this->email->to("{$data['email']['Email']}"); 
            $this->email->subject('Email liên hệ từ website');
            $this->email->message("$email_content");
            $this->email->send();
            
            $data['successfully'] = "Email liên hệ đã được gửi. Cám ơn bạn đã đóng góp ý kiến cho website."; 
            $this->load->view("modcontact-sendMail",$data);
        }
    }
    public function noSendMail(){
        $this->load->model(array("modcontact/mmodcontact_site_contact","modcontact/mmodcontact_site_text"));
        $data['static_page'] = $this->mmodcontact_site_text->getDataByID(6);
        
        $this->form_validation->set_rules("name",lang("frontend.fullname"),"required");
        $this->form_validation->set_rules("content",lang("frontend.content"),"required");
        if($this->form_validation->run() == FALSE){
            $data['successfully'] = "";
            $data['error'] = ""; 
            $this->load->view("modcontact/modcontact-noSendMail",$data);
        }else{
            if($this->input->post("name") == lang("frontend.fullname"). " *"){
                $data['successfully'] = "";
                $data['error'] = lang("frontend.fullname_require"); 
                $this->load->view("modcontact-index",$data);
            }elseif($this->input->post("content") == lang("frontend.content"). " *"){
                $data['successfully'] = "";
                $data['error'] = lang("frontend.content_require"); 
                $this->load->view("modcontact-index",$data);
            }else{
                $add = array(
                        "Name"          => $this->input->post("name"),
                        "Address"       => $this->input->post("address"),
                        "Phone"         => $this->input->post("phone"),
                        "Email"         => $this->input->post("email"),
                        "Content"       => $this->input->post("content"),
                        "Dateset"       => date("Y-m-d H:i:s"),
                        "Status"        => 0,
                     );
                $this->mmodcontact_site_contact->addData($add);
                $data['successfully'] = lang('frontend.send_mail_success'); 
                $this->load->view("modcontact/modcontact-noSendMail",$data);
            }
        }
    }
        
}